package com.panshi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panshi.entity.AppComment;
import com.panshi.entity.AppUser;
import com.panshi.mapper.CommentMapper;
import com.panshi.service.CommentService;
import com.panshi.service.FeignAnswerApiClient;
import com.panshi.service.FeignAppUserClient;
import com.panshi.tools.DataResult;
import com.panshi.tools.PageBean;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 资讯、回答评论表 服务实现类
 * </p>
 *
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired // 用户feign接口
    private FeignAppUserClient feignAppUserClient;

    @Autowired // 回答feign接口
    private FeignAnswerApiClient feignAnswerApiClient;

    @Override
    public DataResult queryCommentByAid(Integer answerId, PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
        List<AppComment> list = commentMapper.queryCommentByAid(answerId); //查询评论
        List<AppComment> listInfo = this.setInfo(list);
        PageInfo pageInfo = new PageInfo(listInfo);
        return new DataResult().setCode(200).setMsg("响应成功").setData(pageInfo);
    }

    @Override
    public DataResult queryCommentById(Integer id, PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
        List<AppComment> list = commentMapper.queryCommentById(id);
        List<AppComment> listInfo = this.setInfo(list);
        PageInfo pageInfo = new PageInfo(listInfo);
        return new DataResult().setCode(200).setMsg("响应成功").setData(pageInfo);
    }

    @Override
    @Cacheable(value = "hasbei:commentInfo", key="#id")
    public AppComment queryCommentInfo(Integer id) {
        return commentMapper.queryCommentInfo(id);
    }

    @Override
    @CacheEvict(cacheNames = "hasbei:commentInfo",allEntries = true)
    public DataResult addComment(AppComment appComment) {
        Integer coverId = appComment.getCoverCommentId();
        if(null == coverId || 0 == coverId){
            appComment.setCommentGrade("1");
        }else{
            appComment.setCommentGrade("2");
        }

        appComment.setCommentStatus("1").setCommentType("a");

        commentMapper.addComment(appComment);

        // 增加回答评论量
        feignAnswerApiClient.updatePariseTread(appComment.getCommentId(),"c");

        return new DataResult().setCode(appComment.getId()).setMsg("增加成功~");
    }

    @Override
    @CacheEvict(cacheNames = "hasbei:commentInfo", key="#id")
    public DataResult delete(Integer id) {

        commentMapper.delete(id);

        AppComment app = commentMapper.queryCommentInfo(id);

        // 减少回答评论量
        feignAnswerApiClient.updatePariseTread(app.getCommentId(), "deleteComment");

        return new DataResult().setCode(id).setMsg("删除成功~");
    }

    /**
     * 修改评论点赞数据
     * @param id 评论id
     */
    public void updateParise(Integer id){
        System.out.println("updateParise: "+id);
        commentMapper.updateParise(id);
    }

    public List<AppComment> setInfo(List<AppComment> list){
        Integer coverUserId = null;// 父级评论用户id
        Integer userId = null; // 评论用户id
        for(AppComment c: list){
            coverUserId = c.getCoverUserId();
            userId = c.getUserId();
            if(userId != null && userId != 0 && userId.equals(c.getUserId())){
                // 查询评论用户信息
                AppUser appUser = feignAppUserClient.queryUserInfo(userId);
                c.setAppUser(appUser);
            }
            if(coverUserId != null && coverUserId != 0 && coverUserId.equals(c.getCoverUserId())){
                // 查询父级评论用户信息
                AppUser coverAppUser = feignAppUserClient.queryUserInfo(coverUserId);
                c.setCoverAppUser(coverAppUser);
            }
        }

        return list;

    }


}
